package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.pojo.bo.system.GroupBO;
import com.lwc.cfdns.pojo.dto.system.GroupDTO;
import com.lwc.cfdns.pojo.entity.SystemGroup;
import com.lwc.cfdns.pojo.vo.system.GroupVO;
import com.lwc.cfdns.service.SystemGroupService;
import com.lwc.cfdns.mapper.SystemGroupMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author spring
 * @description 针对表【cfdns_system_group(后台组合数据表)】的数据库操作Service实现
 * @createDate 2023-07-12 14:30:04
 */
@Service
public class SystemGroupServiceImpl extends ServiceImpl<SystemGroupMapper, SystemGroup>
        implements SystemGroupService {

    /**
     * 创建
     *
     * @param groupDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/12 15:11:05
     */
    @Override
    public Map<String, Object> create(GroupDTO groupDTO) {
        SystemGroup systemGroup = new SystemGroup();
        BeanUtils.copyProperties(groupDTO, systemGroup);
        this.baseMapper.insert(systemGroup);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemGroup.getId());
        return map;
    }

    /**
     * 修改
     *
     * @param groupDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/12 15:15:18
     */
    @Override
    public Map<String, Object> update(GroupDTO groupDTO) {
        SystemGroup systemGroup = this.baseMapper.selectById(groupDTO.getId());
        Assert.notNull(systemGroup, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(groupDTO, systemGroup);
        int i = this.baseMapper.updateById(systemGroup);
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return GroupVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/12 15:15:59
     */
    @Override
    public GroupVO view(Long id) {
        SystemGroup systemGroup = this.baseMapper.selectById(id);
        Assert.notNull(systemGroup, ResultCode.NOT_DATA_FOUNT);
        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(systemGroup, groupVO);
        // 处理rule(param参数)
        String[] arr = {"select", "radio", "checkbox"};
        String[] inputArr = {"textarea", "input", "editor", "date", "datetime"};
        List<Map<String, Object>> fields = (List<Map<String, Object>>) groupVO.getFields();
        for (Map<String, Object> field : fields) {
            String param = ConvertUtils.toStr(field.get("param"));
            String type = ConvertUtils.toStr(field.get("type"));
            ArrayList<Map<String, Object>> ruleArr = new ArrayList<>();
            field.put("keyRuleArr", ruleArr);
            field.put("keyRule", "");
            if (StringUtils.isNotEmpty(param) && Arrays.asList(arr).contains(type)) {
                String[] strings = ConvertUtils.toStrArray("\\n", param);
                for (String s : strings) {
                    Map<String, Object> map = new HashMap<>();
                    String[] strings1 = ConvertUtils.toStrArray(":", s);
                    map.put("value", strings1[0]);
                    map.put("label", strings1[1]);
                    ruleArr.add(map);
                }
                // select,radio,checkbox等类型使用的数组
                field.put("keyRuleArr", ruleArr);
            } else if (StringUtils.isNotEmpty(param) && Arrays.asList(inputArr).contains(type)) {
                // 输入框型的placeholder
                field.put("keyRule", param);
            }
//            field.remove("param");
        }
        return groupVO;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/12 15:19:56
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemGroup>().in(SystemGroup::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param groupBO
     * @return IPage<SystemGroup>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/12 15:22:21
     */
    @Override
    public IPage<SystemGroup> lists(GroupBO groupBO) {
        Page<SystemGroup> groupPage = this.baseMapper.selectPage(groupBO.toPage(), groupBO.toQueryWrapper());

        // 按OV返回
        groupPage.convert(s -> {
            GroupVO groupVO = new GroupVO();
            BeanUtils.copyProperties(s, groupVO);
            return groupVO;
        });
        return groupPage;
    }
}




